<?PHP
/**
 *  AJAX LISTENER
 *
 *  The script runs all applications' "listener" method in JS periodically.
 *
 *  IMPORTANT: All listener methods only return javascript sunction/functions
 *  or nothing.
 *
 */
set_time_limit(30);
header("Cache-control: private");
// all subdomains save in one session
$domain = array_reverse(explode('.', $_SERVER['HTTP_HOST']));
ini_set('session.cookie_domain', '.' . $domain[1] . '.' . $domain[0]);

session_start();
define('TOKEN', session_id());

// create empty $data object
$data = new stdClass();

// include configuration
include 'config.php';

// scan and include apps classes
foreach ($data->APPS as $i)
    foreach (glob('apps/' . $i . '/lib/*.php') as $j)
        include $j;

// ACCEPT LANGUAGES / LOCALES  apps/home/lib/classes/classWorld.php
list($locale, $lang) = world::locale(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 5), (@$_GET['lang'] ? $_GET['lang'] : @$_SESSION['lang']));
define('LANG', @$_SESSION['lang'] = $lang);
define('LOCALE', $locale);
// link prefix if subdomain ? [domain + lang] : lang
define('PREFIX', count($domain[0] == 'blog') > 2 ? 'http://' . DOMAIN . '/' . LANG : '/' . LANG);
// supported language list
$data->LANGUAGES = world::languages();

// set request data
define('FORMAT', 'json');
define('PARAMS', @$_GET['params']);

// MULTI LANGUAGE with PHP_GETTEXT
setlocale(LC_ALL, LOCALE . '.UTF-8');
// bind language files
if ($data->APPS)
    foreach ($data->APPS as $i)
        bindtextdomain($i, './apps/' . $i . '/locale');

/* --------------------------------------------------------------
  RUN LISTENER FUNCTIONS
  -------------------------------------------------------------- */
// open db connection
db::connect();
foreach ($data->APPS as $i)
    if (method_exists($i, 'listener')) {
        $r .= $i::listener();
    }
// close db connection
db::disconnect();
unset ($data);
// return JS
die('<script>'.$r.'</script>');
?>